var app = getApp();
Page( Object.assign({},{
    data:{
        wxuser : null,
        order : null,
        sets : null,
        qrcode : null,
        options : null,
        animationa : null,
        animationData : {},
        showprize : false,
        chouing : false,
        express : [],
        goodgcard : [],
        isshowgcard : false,
        psorder : false,
        isIPX : app.globalData.isIPX,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        datearr : {},
    },
    onLoad:function(options){
        var self = this;
        self.setData({
            options : options,
        });
        app.com.auth(function(user){
            app.com.http('order','POST',{oid:options.id,op:'orderinfo'},0,false,'','',function(res){
                if (!res.data.errno) {
                    self.setData({
                        Pagetheme : res.data.data.params.style,
                        order : res.data.data.order,
                        good : res.data.data.good,
                        shop : res.data.data.shop,
                        group : res.data.data.group,
                        inited : true,
                        params : res.data.data.params,
                        prize : res.data.data.prize,
                        hxlog : res.data.data.hxlog ? res.data.data.hxlog : [],
                        showprize : res.data.data.prize.isprize == 1 ? true : false,
                        bindorder : res.data.data.bindorder ? res.data.data.bindorder : [],
                        studylist : res.data.data.studylist ? res.data.data.studylist : [],
                        goodgcard : res.data.data.goodgcard ? res.data.data.goodgcard : [],
                        psorder : res.data.data.psorder ? res.data.data.psorder : false,
                    },function(){
                        if(self.data.goodgcard.length > 0){
                            self.setData({
                                isshowgcard : true,
                            })
                        }
                        if( res.data.data.prize.isprize == 1 ){
                            self.selectComponent("#payorderprize").getLottery(res.data.data.prize,res.data.data.order.id);
                        }
                        if(res.data.data.showchb == 1){
                            self.selectComponent("#paychb").showhb(res.data.data.showchboid,res.data.data.showchbfee);
                        }
                    });


                    if( res.data.data.order.exname && res.data.data.order.exnum ){
                        var postdata = {
                            name : res.data.data.order.exname,
                            num : res.data.data.order.exnum,
                            op:'express'
                        };

                        app.com.http('order','POST',postdata,0,false,'','',function(res){
                            if (!res.data.errno && res.data) {
                                self.setData({
                                    express : res.data.data
                                })
                            }
                        });
                    }
                }else{
                    app.com.alert(res.data.message);
                }
            });
        });
        wx.setNavigationBarTitle({
          title:  '订单详情',
        });

    },
    dealorder( e ){
        var self = this;
        var id = self.data.order.orderid;
        var type = e.currentTarget.dataset.type;
        var postdata = {
            oid : id,
            op:type
        };
        if( type == 'cancel' || type == 'com' ){
            var nostr = '确定取消订单吗？';
            if( type == 'com' ) {
                var nostr = '确定完成订单吗？';
            }
            app.com.confirm(nostr,function(){
                app.com.http('order','POST',postdata,0,true,'','',function(res){
                    if ( !res.data.errno && res.data.errno !== undefined ) {
                        if( type == 'cancel' ){
                            app.com.toast('已取消');
                        }else{
                            self.setData({
                                'order.status' : 3,
                            });
                            app.com.toast(res.data.message);
                        }
                    }else{
                        app.com.alert(res.data.message);
                    }
                });
            });
        }else if( type == 'refund' ){
        	var postdata = {
        		op : 'applyback',
        		oid : self.data.order.id
        	}
            app.com.confirm('确定申请退款吗？',function(){
                app.com.http('order','POST',postdata,0,true,'','',function(res){
                    if ( !res.data.errno && res.data.errno !== undefined ) {
                        
                        self.setData({
                            'order.status' : 1,
                            'order.iscanrefund' : 0,
                            'order.statusstr' : '申请退款中',
                        });
                        app.com.toast(res.data.message);
                        
                    }else{
                        app.com.alert(res.data.message);
                    }
                });
            });
        }else if( type == 'pay' || type == 'com' || type == 'express' || type == 'djpay' ){
            app.com.http('order','POST',postdata,0,true,'','',
            function(res){
                if ( !res.data.errno && res.data.errno !== undefined ) {
                    if( type == 'pay' || type == 'djpay' ){

                        if(res.data.data.payed == 1){
                            app.com.toast('支付完成','success',function(){
                                if( self.data.order.gtype == 2 ){
                                    self.checkGroup(self.data.order.orderid);
                                }else{
                                    self.onLoad(self.data.options);
                                }
                            });
                            return false;
                        }

                        wx.requestPayment({
							'appId' : res.data.data.appId,
                            'timeStamp': res.data.data.timeStamp,
                            'nonceStr': res.data.data.nonceStr,
                            'package': res.data.data.package,
                            'signType': 'MD5',
                            'paySign': res.data.data.paySign,
                            'success': function (payres) {
                                if( payres.errMsg == 'requestPayment:ok' ){
                                    app.com.toast('支付完成','success',function(){
                                        if( self.data.order.gtype == 2 ){
                                            self.checkGroup(self.data.order.orderid);
                                        }else{
                                            self.onLoad(self.data.options);
                                        }
                                    });
                                }
                            },
                            'fail': function (payres) {
                                console.log(payres);
                            }
                        });
                    }
                    if( type == 'com' ) {
                        self.setData({
                            'order.status' : 3,
                        });
                    }
                }else{
                    app.com.alert(res.data.message);
                }
            });

        }else if( type == 'toshop' ){
            wx.openLocation({
              latitude:  parseFloat(self.data.sets.shoplat),
              longitude: parseFloat(self.data.sets.shoplng),
              scale: 13
            });
        }else if( type == 'call' ){
            wx.makePhoneCall({
              phoneNumber: self.data.sets.shoptel
            });
        }else if( type == 'comment' ){
            wx.navigateTo({
              url: '/zofui_sales/order/comment?oid='+self.data.order.orderid
            });
        }else if( type == 'yydate' ){
            if(self.data.params.xcxmess && wx.requestSubscribeMessage){
                wx.requestSubscribeMessage({
                    tmplIds : self.data.params.xcxmess,
                    complete : function(){
                        self.yyfb();
                    }
                })
            }else{
                self.yyfb();
            }
        }
    },
    yyfb(){
        var self = this;
        var postdata = {
            oid : this.data.order.id,
            op : 'getyydate',
        }
        app.com.http('order','POST',postdata,0,true,'','',function(res){
            if (res.data && !res.data.errno && res.data.errno !== undefined ) {
                self.setData({
                    datearr : {
                        actgid : res.data.data.gid,
                        date : res.data.data.dateday,
                        time : res.data.data.datetimes,
                        isprice : 0,
                    }
                },function(){
                    self.selectComponent('#showdate').showthis();
                })
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    onPullDownRefresh: function () {
        this.onLoad( this.data.options );
        wx.stopPullDownRefresh();
    },
    checkGroup(orderid){
        var self = this;
        setTimeout(function(){
            app.com.http('confirm','POST',{orderid:orderid,op:'checkgroup'},0,false,'','',function(res){
                if (!res.data.errno) {
                    var tourl = '/zofui_sales/group/group?id='+res.data.data.groupid;
                    wx.navigateTo({
                      url: tourl
                    });
                }else{
                    self.checkGroup(orderid);
                }
            });
        },500);
    },
    getedchb(){
    },
    cancelorder(){
        var self = this;
        var id = self.data.order.orderid;
        var postdata = {
            oid : id,
            op : 'cancel'
        };
        app.com.confirm('确定取消订单吗？',function(){
            app.com.http('good_v1','POST',postdata,0,true,'','',function(res){
                if ( !res.data.errno && res.data.errno !== undefined ) {
                    app.com.toast(res.data.message,'none',function(){
                        self.setData({
                            'order.unset' : 1,
                        })
                        wx.navigateBack();
                    });
                }else{
                    app.com.alert(res.data.message);
                }
            });
        });
    },
    onClosecard(){
        this.setData({
            isshowgcard : false,
        });
    },
    confirmdate(e){
        var self = this;
        var postdata = {
            oid : this.data.order.id,
            op : 'setyydate',
            date : e.detail.date,
            time : e.detail.time ? e.detail.time : '',
        }
        app.com.confirm('确定预约'+postdata.date+' '+postdata.time+'吗？',function(){
            app.com.http('order','POST',postdata,0,true,'','',function(res){
                if (res.data && !res.data.errno && res.data.errno !== undefined ) {
                    app.com.alert(res.data.message);
                    self.setData({
                        'order.isneedyy' : 2,
                    })
                    self.onLoad( self.data.options );
                }else{
                    app.com.alert(res.data.message);
                }
            });
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },
    catchtouchmove : function(){
        return false;
    },
    callphone : function(e){
        app.com.callphone( e );
    },
    showkefuimg : function(e){
        app.com.alert('长按图片识别二维码联系客服',function(){
            wx.previewImage({
                current: e.currentTarget.dataset.img,
                urls: [e.currentTarget.dataset.img]
            });
        });
    },
    copy(e){
        wx.setClipboardData({
            data : e.currentTarget.dataset.copy
        })
    },
    FormIdsubmit : function(e){
        if( app.globalData.formarr.length <= 20 ) app.globalData.formarr.push(e.detail.formId);
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    }, 

}))